<?php

namespace app\admin\controller\qingdongwms\general;

use addons\qingdongwms\model\FormField;
use app\admin\controller\qingdongwms\Base;
use addons\qingdongwms\model\CustomerType;
use app\admin\model\AuthGroup;
use fast\Tree;

/**
 * 常规设置
 */
class Adminconfig extends Base {
    protected $relationSearch = true;
    /**
     * @var \addons\qingdongwms\model\AdminConfig
     */
    protected $model       = null;


    public function _initialize() {
        parent::_initialize();
        $this->model = new \addons\qingdongwms\model\AdminConfig;
    }


    /**
     * 查看
     */
    public function index() {
        return $this->view->fetch();
    }

    //设置客户到期时间
    public function examine(){
        //设置过滤方法
        if ($this->request->isAjax()) {

            $customer_type             = $this->request->post('customer_type/a', '', 'trim');
            foreach ($customer_type as $k => $v) {
                $find = CustomerType::where([ 'name' => $k])->find();
                if($find){
                    CustomerType::where([ 'name' => $k])->update(['cus_status' => $v['cus_status']??0, 'days_cus' => $v['days_cus']]);
                }else{
                    $res=array(
                        'name'=>$k,
                        'days_cus'=>$v['days_cus']??0,
                        'days_pid'=>$v['days_pid']??0,
                        'cus_status'=>$v['cus_status']??0,
                    );
                    CustomerType::create($res);
                }
            }

            $this->success('设置成功');
        }
        $customerType=CustomerType::where([])->column('days_cus,days_pid,cus_status,pid_status','name');
        if(!key_exists('A',$customerType)){
            $customerType['A']=array(
                'name'=>'A',
                'days_cus'=>0,
                'days_pid'=>0,
                'cus_status'=>0,
            );
        }
        if(!key_exists('B',$customerType)){
            $customerType['B']=array(
                'name'=>'B',
                'days_cus'=>0,
                'days_pid'=>0,
                'cus_status'=>0,
            );
        }
        if(!key_exists('C',$customerType)){
            $customerType['C']=array(
                'name'=>'C',
                'days_cus'=>0,
                'days_pid'=>0,
                'cus_status'=>0,
            );
        }
        if(!key_exists('D',$customerType)){
            $customerType['D']=array(
                'name'=>'D',
                'days_cus'=>0,
                'days_pid'=>0,
                'cus_status'=>0,
            );
        }
        $this->assign('customer_type',$customerType);
        return $this->view->fetch();
    }
    //设置反馈节点
    public function feedback(){
        //设置过滤方法
        if ($this->request->isAjax()) {
            $feedback            = $this->request->post('feedback/a', '', 'trim');
            $data = CustomerType::where(['deletetime'=>null])->find();
            if($data){
                CustomerType::where(['deletetime'=>null])
                    ->update(['pid_status' => $feedback['pid_status']??0, 'days_pid' => $feedback['days_pid']??0]);
            }else{
                CustomerType::insert(['pid_status' => $feedback['pid_status']??0, 'days_pid' => $feedback['days_pid']??0]);
            }


            $this->success('设置成功');
        }
        $feedback=CustomerType::where([])->find();
        $feedback['pid_status'] = isset($feedback['pid_status'])?$feedback['pid_status']:0;
        $feedback['days_pid'] = isset($feedback['days_pid'])?$feedback['days_pid']:0;
        $this->assign('feedback',$feedback);
        return $this->view->fetch();
    }


    //微信小程序
    public function wxprogram() {
        //设置过滤方法
        if ($this->request->isAjax()) {

            $row              = $this->request->post('row/a', '', 'trim');
            $mini_appid       = $row['mini_appid'] ?? 0;
            $mini_secret      = $row['mini_secret'] ?? 0;
            $this->model::setConfig('mini_appid', $mini_appid, $this->model::TYPE_WECHAT);
            $this->model::setConfig('mini_secret', $mini_secret, $this->model::TYPE_WECHAT);
            $this->success('设置成功');
        }
        $wechat = $this->model->where([
            'type'     => $this->model::TYPE_WECHAT
        ])->column('field,value');
        $wechat['mini_appid'] = isset($wechat['mini_appid']) ? $wechat['mini_appid'] : '';
        $wechat['mini_secret'] = isset($wechat['mini_secret']) ? $wechat['mini_secret'] : '';
        $this->assign('wechat', $wechat);

        return $this->view->fetch();
    }
    //微信公众号
    public function weixin() {
        //设置过滤方法
        if ($this->request->isAjax()) {

            $row              = $this->request->post('row/a', '', 'trim');
            $appid            = $row['appid'] ?? 0;
            $secret           = $row['secret'] ?? 0;
            $this->model::setConfig('appid', $appid, $this->model::TYPE_WECHAT);
            $this->model::setConfig('secret', $secret, $this->model::TYPE_WECHAT);

            $this->success('设置成功');
        }
        $wechat = $this->model->where([
            'type'     => $this->model::TYPE_WECHAT
        ])->column('field,value');
        $wechat['appid'] = isset($wechat['appid']) ? $wechat['appid'] : '';
        $wechat['secret'] = isset($wechat['secret']) ? $wechat['secret'] : '';
        $this->assign('wechat', $wechat);

        return $this->view->fetch();
    }
    //公海回收
    public function sea() {
        //设置过滤方法
        if ($this->request->isAjax()) {

            $row             = $this->request->post('row/a', '', 'trim');
            $auto            = $row['auto'] ?? 0;
            $genjing         = $row['genjing'] ?? 0;
            $genjing_success = $row['genjing_success'] ?? 0;
            $genjing_day     = $row['genjing_day'] ?? 0;
            $chengjiao       = $row['chengjiao'] ?? 0;
            $chengjiao_day   = $row['chengjiao_day'] ?? 0;
            $genjing_c_name   = $row['genjing_c_name'] ?? '';
            $chengjiao_c_name   = $row['chengjiao_c_name'] ?? '';
            $this->model::setConfig('auto', $auto, $this->model::TYPE_SEAS);
            $this->model::setConfig('genjing', $genjing, $this->model::TYPE_SEAS);
            $this->model::setConfig('genjing_c_name', $genjing_c_name, $this->model::TYPE_SEAS);
            $this->model::setConfig('genjing_success', $genjing_success, $this->model::TYPE_SEAS);
            $this->model::setConfig('genjing_day', $genjing_day, $this->model::TYPE_SEAS);
            $this->model::setConfig('chengjiao', $chengjiao, $this->model::TYPE_SEAS);
            $this->model::setConfig('chengjiao_day', $chengjiao_day, $this->model::TYPE_SEAS);
            $this->model::setConfig('chengjiao_c_name', $chengjiao_c_name, $this->model::TYPE_SEAS);

            $this->success('设置成功');
        }
        $seas = $this->model->where(['type'=>$this->model::TYPE_SEAS])->column('field,value');
        $seas['auto'] = isset($seas['auto']) ? $seas['auto'] : '';
        $seas['genjing'] = isset($seas['genjing']) ? $seas['genjing'] : '';
        $seas['genjing_success'] = isset($seas['genjing_success']) ? $seas['genjing_success'] : '';
        $seas['genjing_day'] = isset($seas['genjing_day']) ? $seas['genjing_day'] : '';
        $seas['chengjiao'] = isset($seas['chengjiao']) ? $seas['chengjiao'] : '';
        $seas['chengjiao_day'] = isset($seas['chengjiao_day']) ? $seas['chengjiao_day'] : '';

        $customerType=CustomerType::where([])->column('days_cus,days_pid,cus_status,pid_status','name');
        $feedback=CustomerType::where([])->find();
        $this->assign('seas',$seas);
        $this->assign('customer_type',$customerType);
        $this->assign('feedback',$feedback);
        return $this->view->fetch();
    }
    //腾讯地图
    public function map() {
        //设置过滤方法
        if ($this->request->isAjax()) {

            $row              = $this->request->post('row/a', '', 'trim');
            $this->model::setConfig('map_key', $row['map_key'], $this->model::TYPE_WECHAT);
            $this->success('设置成功');
        }
        $wechat = $this->model->where([
            'type'     => $this->model::TYPE_WECHAT
        ])->column('field,value');
        if (empty($wechat['map_key'])) {
            $wechat['map_key'] = '';
        }
        $this->assign('wechat', $wechat);

        return $this->view->fetch();
    }
    //百度OCR
    public function baidu() {
        //设置过滤方法
        if ($this->request->isAjax()) {

            $row              = $this->request->post('row/a', '', 'trim');
            $mini_appid       = $row['baidu_api'] ?? 0;
            $mini_secret      = $row['baidu_secret'] ?? 0;
            $this->model::setConfig('baidu_api', $mini_appid, $this->model::TYPE_BAIDU);
            $this->model::setConfig('baidu_secret', $mini_secret, $this->model::TYPE_BAIDU);
            $this->success('设置成功');
        }
        $wechat = $this->model->where([
            'type'     => $this->model::TYPE_BAIDU
        ])->column('field,value');
        $wechat['baidu_api'] = isset($wechat['baidu_api']) ? $wechat['baidu_api'] : '';
        $wechat['baidu_secret'] = isset($wechat['baidu_secret']) ? $wechat['baidu_secret'] : '';
        $this->assign('wechat', $wechat);

        return $this->view->fetch();
    }
    //textin名片识别
    public function textin() {
        //设置过滤方法
        if ($this->request->isAjax()) {

            $row              = $this->request->post('row/a', '', 'trim');
            $field=$this->request->post('field/a','','trim');
            $mini_appid       = $row['textin_api'] ?? 0;
            $mini_secret      = $row['textin_secret'] ?? 0;
            $this->model::setConfig('textin_api', $mini_appid, $this->model::TYPE_TEXTIN);
            $this->model::setConfig('textin_secret', $mini_secret, $this->model::TYPE_TEXTIN);
            $this->model::setConfig('textin_field', json_encode($field,JSON_UNESCAPED_UNICODE), $this->model::TYPE_TEXTIN);
            $this->success('设置成功');
        }
        $wechat = $this->model->where([
            'type'     => $this->model::TYPE_TEXTIN
        ])->column('field,value');
        $wechat['textin_api'] = isset($wechat['textin_api']) ? $wechat['textin_api'] : '';
        $wechat['textin_secret'] = isset($wechat['textin_secret']) ? $wechat['textin_secret'] : '';
        $wechat['textin_field'] = isset($wechat['textin_field']) ? json_decode($wechat['textin_field'],true) : '';

        $fields=FormField::where(['types'=>FormField::CUSTOMER_TYPE,'info_type'=>'main'])->select();
        $this->assign('fields', $fields);
        $this->assign('wechat', $wechat);

        return $this->view->fetch();
    }
    //聚合数据
    public function juhe() {
        //设置过滤方法
        if ($this->request->isAjax()) {

            $row              = $this->request->post('row/a', '', 'trim');
            $mini_appid       = $row['juhe_api'] ?? 0;
            $mini_secret      = $row['juhe_secret'] ?? 0;
            $this->model::setConfig('juhe_api', $mini_appid, $this->model::TYPE_JUHE);
            $this->model::setConfig('juhe_secret', $mini_secret, $this->model::TYPE_JUHE);
            $this->success('设置成功');
        }
        $wechat = $this->model->where([
            'type'     => $this->model::TYPE_JUHE
        ])->column('field,value');
        $wechat['juhe_api'] = isset($wechat['juhe_api']) ? $wechat['juhe_api'] : '';
        $wechat['juhe_secret'] = isset($wechat['juhe_secret']) ? $wechat['juhe_secret'] : '';
        $this->assign('wechat', $wechat);

        return $this->view->fetch();
    }
    //客户端设置
    public function user() {
        //设置过滤方法
        if ($this->request->isAjax()) {
            $row              = $this->request->post('row/a', '', 'trim');
            $this->model::setConfig('user_name', $row['user_name'], $this->model::TYPE_USER);
            $this->success('设置成功');
        }
        $wechat = $this->model->where([
            'type'     => $this->model::TYPE_USER
        ])->column('field,value');
        $wechat['user_name'] = isset($wechat['user_name']) ? $wechat['user_name'] : '';
        $this->assign('wechat', $wechat);

        return $this->view->fetch();
    }
    //物流
    public function logist() {
        //设置过滤方法
        if ($this->request->isAjax()) {
            $row              = $this->request->post('row/a', '', 'trim');
            $this->model::setConfig('logist_key', $row['logist_key'], $this->model::TYPE_JUHE);
            $this->success('设置成功');
        }
        $wechat = $this->model->where([
            'type'     => $this->model::TYPE_JUHE
        ])->column('field,value');
        $wechat['logist_key'] = isset($wechat['logist_key']) ? $wechat['logist_key'] : '';
        $this->assign('wechat', $wechat);

        return $this->view->fetch();
    }
    //邮箱
    public function email() {
        //设置过滤方法
        if ($this->request->isAjax()) {
            $row              = $this->request->post('row/a', '');
            $this->model::setConfig('email_key', $row['email_key'], $this->model::TYPE_WECHAT);
            $this->success('设置成功');
        }
        $wechat = $this->model->where([
            'type'     => $this->model::TYPE_WECHAT
        ])->column('field,value');
        $wechat['email_key'] = isset($wechat['email_key']) ? $wechat['email_key'] : '';
        $this->assign('wechat', $wechat);

        return $this->view->fetch();
    }
    //企业微信
    public function wework() {
        //设置过滤方法
        if ($this->request->isAjax()) {

            $row              = $this->request->post('row/a', '', 'trim');
            $corpid            = $row['corpid'] ?? 0;
            $corpsecret           = $row['corpsecret'] ?? 0;
            $agentid           = $row['agentid'] ?? 0;
            $this->model::setConfig('corpid', $corpid, $this->model::TYPE_WECHAT);
            $this->model::setConfig('corpsecret', $corpsecret, $this->model::TYPE_WECHAT);
            $this->model::setConfig('agentid', $agentid, $this->model::TYPE_WECHAT);
            $this->success('设置成功');
        }
        $wechat = $this->model->where([
            'type'     => $this->model::TYPE_WECHAT
        ])->column('field,value');
        $wechat['corpid'] = isset($wechat['corpid']) ? $wechat['corpid'] : '';
        $wechat['corpsecret'] = isset($wechat['corpsecret']) ? $wechat['corpsecret'] : '';
        $wechat['agentid'] = isset($wechat['agentid']) ? $wechat['agentid'] : '';
        $this->assign('wechat', $wechat);

        return $this->view->fetch();
    }
    //钉钉
    public function dingding() {
        //设置过滤方法
        if ($this->request->isAjax()) {

            $row              = $this->request->post('row/a', '', 'trim');
            $ding_key            = $row['ding_key'] ?? 0;
            $ding_secret           = $row['ding_secret'] ?? 0;
            $this->model::setConfig('ding_key', $ding_key, $this->model::TYPE_DING);
            $this->model::setConfig('ding_secret', $ding_secret, $this->model::TYPE_DING);
            $this->success('设置成功');
        }
        $wechat = $this->model->where([
            'type'     => $this->model::TYPE_DING
        ])->column('field,value');
        $wechat['ding_key'] = isset($wechat['ding_key']) ? $wechat['ding_key'] : '';
        $wechat['ding_secret'] = isset($wechat['ding_secret']) ? $wechat['ding_secret'] : '';
        $this->assign('dingding', $wechat);

        return $this->view->fetch();
    }
    //线索分配
    public function leads(){
        $this->childrenAdminIds = $this->auth->getChildrenAdminIds($this->auth->isSuperAdmin());
        $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin());

        //角色组
        $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
        Tree::instance()->init($groupList);
        $groupdata = [];
        if ($this->auth->isSuperAdmin()) {
            $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
            foreach ($result as $k => $v) {
                $groupdata[$v['id']] = $v['name'];
            }
        } else {
            $result = [];
            $groups = $this->auth->getGroups();
            foreach ($groups as $m => $n) {
                $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
                $temp = [];
                foreach ($childlist as $k => $v) {
                    $temp[$v['id']] = $v['name'];
                }
                $result[__($n['name'])] = $temp;
            }
            $groupdata = $result;
        }
        //设置过滤方法
        if ($this->request->isAjax()) {
            $row = $this->request->post('row/a', '', 'trim');

            $group = $this->request->post("group/a");
            //过滤不允许的组别,避免越权
            $group = array_intersect($this->childrenGroupIds, $group);
            $department='';
            if ($group) {
                $department=implode(',',$group);
            }
            $this->model::setConfig('leadauto', $row['leadauto'] ?? 0, $this->model::TYPE_LEAD);
            $this->model::setConfig('lead_day', $row['lead_day'] ?? 0, $this->model::TYPE_LEAD);
            $this->model::setConfig('leadcard', $row['leadcard'] ?? 0, $this->model::TYPE_LEAD);
            $this->model::setConfig('department', $department ?? 0, $this->model::TYPE_LEAD);

            $this->success('设置成功');
        }

        $lead = $this->model->where(['type'=>$this->model::TYPE_LEAD])->column('field,value');
        $lead['leadauto'] = isset($lead['leadauto']) ? $lead['leadauto'] : 0;
        $lead['lead_day'] = isset($lead['lead_day']) ? $lead['lead_day'] : 1;
        $lead['leadcard'] = isset($lead['leadcard']) ? $lead['leadcard'] : 2;
        $lead['department'] = isset($lead['department']) ? $lead['department'] : 0;



        $this->view->assign('groupdata', $groupdata);
        $this->assign('lead',$lead);
        return $this->view->fetch();
    }
    //手机号查重
    public function phones(){
        //设置过滤方法
        if ($this->request->isAjax()) {
            $row = $this->request->post('row/a', '', 'trim');
            $this->model::setConfig('is_linkman', $row['is_linkman'] ?? 0, $this->model::TYPE_SEAS);
            $this->success('设置成功');
        }
        $seas = $this->model->where(['type'=>$this->model::TYPE_SEAS])->column('field,value');
        $seas['is_linkman'] = isset($seas['is_linkman']) ? $seas['is_linkman'] : 0;

        $this->assign('seas',$seas);
        return $this->view->fetch();
    }
}
